<template>
  <div>
    <OrderHeader title="我的购物车">
      <template slot="tip">
        <span>温馨提示：产品是否购买成功，以最终下单为准哦，请尽快结算</span>
      </template>
    </OrderHeader>
    <div class="cart">
      <div class="container">
        <div class="cart-box">
          <ul class="cart-item-head">
            <li class="col">
              <input
                class="checkbox"
                type="checkbox"
                :checked="allChecked&&list.length>0"
                @change="quanxuan"
              /><span>全选</span>
            </li>
            <li class="col-1">商品图片</li>
            <li class="col-3">商品名称</li>
            <li class="col-1">单价</li>
            <li class="col-2">数量</li>
            <li class="col-1">小计</li>
            <li class="col-1">操作</li>
          </ul>
          <ul class="cart-item-list">
            <li class="cart-item" v-for="(item, index) in list" :key="index">
              <Modal
                title="提示"
                sureText="确定"
                btnType="1"
                modalType="middle"
                v-show="showModal"
                @close="showModal = false"
                @submit="delPro(item)"
              >
                <template slot="body">
                  <p>您确定要删除</p>
                </template>
              </Modal>
              <div class="item-check">
                <input
                  type="checkbox"
                  :checked="item.productSelected"
                  class="checkbox"
                  @change="updateCart(item)"
                />
              </div>
              <div class="item-img">
                <img v-lazy="item.productMainImage" alt="" />
              </div>
              <div class="item-name">
                <span>{{
                  item.productName + " , " + item.productSubtitle
                }}</span>
              </div>
              <div class="item-price">{{ item.productPrice }}</div>
              <div class="item-num">
                <div class="num-box">
                  <a
                    href="javascript:void(0)"
                    class="mins"
                    @click="updateCart(item, '-')"
                    >-</a
                  >
                  <!-- <input
                    autocomplete="off"
                    type="text"
                    class="itxt"
                    :value="item.quantity"
                  /> -->
                  <span>{{ item.quantity }}</span>
                  <a
                    href="javascript:void(0)"
                    class="plus"
                    @click="updateCart(item, '+')"
                    >+</a
                  >
                </div>
              </div>
              <div class="item-total">{{ item.productTotalPrice }}</div>
              <div class="item-del" @click="showModal = true"></div>
            </li>
          </ul>
        </div>
        <div class="order-wrap clearfix">
          <div class="cart-tip fl">
            <a href="/">继续购物</a>
            共<span>{{ totalLength }} </span>件商品，已选择<span>{{
              checkedNum
            }}</span
            >件
          </div>
          <div class="total fr">
            合计：<span>{{ cartTotalPrice }}</span
            >元
            <a href="javascript:;" class="btn" @click="xuanze">去结算</a>
          </div>
        </div>
      </div>
    </div>

    <BarService />
    <Footer />
  </div>
</template>

<script>
import requests from "@/api/ajax";
export default {
  name: "ShopCart",

  data() {
    return {
      showModal: false,
      list: [], //商品列表
      allChecked: false, //是否全选
      cartTotalPrice: 0, //商品总金额
      sum: 0,
      //选中商品数量
    };
  },

  mounted() {
    this.getCartList();
  },
  // //判断底部复选框是否勾选【全部产品都选中，采勾选】

  computed: {
    totalLength() {
      let sum = 0;
      this.list.forEach((item) => {
        sum += item.quantity;
      });
      return sum;
    },
    checkedNum() {
      let sum = 0;
      let newlist = this.list.filter((item) => item.productSelected);
      newlist.forEach((item) => {
        sum += item.quantity;
      });
      return sum;
        //   this.cartList.map((item)=>{
        //   this.count += item.quantity;
        // })
    },
  },

  methods: {
    xuanze() {
      // 返回布尔值如果是真则数组里面每一个元素都没被选择
      let choice = this.list.every((item) => !item.productSelected);
      if (choice) {
         this.$message.error("请选择至少一件商品");
      } else {
        this.$router.push("/order/confirm");
      }
    },
    delPro(item) {
      requests.delete(`/carts/${item.productId}`).then((res) => {
        this.renderData(res);
        this.showModal = false;
         this.$message.success("删除成功")
      });
    },

    updateCart(item, type) {
      let quantity = item.quantity,
        selected = item.productSelected;
      if (type == "-") {
        if (quantity == 1) {
          this.$message.error("商品至少保留一件");
          return;
        }
        quantity--;
      } else if (type == "+") {
        if (quantity > item.productStock) {
        this.$message.error("购买数量不能超过库存数量");
          return;
        }
        quantity++;
      } else {
        selected = !item.productSelected;
      }

      requests
        .put(`/carts/${item.productId}`, {
          quantity,
          selected,
        })
        .then((res) => {
          this.renderData(res);
        });
    },
    renderData(res) {
      this.list = res.data.cartProductVoList || [];
      this.allChecked = res.data.selectedAll;
      this.cartTotalPrice = res.data.cartTotalPrice;
    },
    getCartList() {
      requests.get("/carts").then((res) => {
        this.renderData(res);
        // 选择有选择地长度
      });
    },
    quanxuan() {
      let url = this.allChecked ? "/carts/unSelectAll" : "/carts/selectAll";
      requests.put(url).then((res) => {
        this.renderData(res);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../assets/scss/mixin";
.cart {
  background-color: #f5f5f5;
  padding-top: 30px;
  padding-bottom: 114px;

  .cart-box {
    background-color: #fff;
    font-size: 14px;
    color: #999999;
    text-align: center;
    .checkbox {
      display: inline-block;
      width: 22px;
      height: 22px;
      border: 1px solid #e5e5e5;
      vertical-align: middle;
      margin-right: 17px;
      cursor: pointer;
    }
    .cart-item-head {
      display: flex;
      height: 79px;
      line-height: 79px;
      .col {
        width: 13%;
      }
      .col-1 {
        flex: 1;
      }
      .col-2 {
        flex: 2;
      }
      .col-3 {
        flex: 3;
      }
    }
    .cart-item-list {
      .cart-item {
        display: flex;
        align-items: center;
        height: 125px;
        border-top: 1px solid #e5e5e5;
        font-size: 16px;
        .item-check {
          flex: 1;
        }
        .item-img {
          flex: 1; img {
            width: 55px;
            height:55px;
            vertical-align: middle;
          }
        }
        .item-name {
          flex: 2;
          font-size: 18px;
          color: #333333;
          display: flex;
          align-items: center;
         
          span {
            margin-left: 30px;
          }
        }
        .item-price {
          flex: 1;
          color: #333333;
        }
        .item-num {
          flex: 1;
          .num-box {
            display: flex;
            justify-content: space-between;
            width: 150px;
            height: 40px;
            line-height: 40px;
            border: 1px solid #e5e5e5;
            a {
              text-align: center;

              flex: 1;
              color: #333333;
            }

            input {
              border: 1px solid #ddd;

              width: 40px;
              height: 40px;

              text-align: center;
              font-size: 14px;
            }
          }
        }
        .item-total {
          flex: 1;
          color: #ff6600;
        }
        .item-del {
          flex: 1;
          @include bgImg(14px, 12px, "../../../public/imgs/icon-close.png")
            display: block;
          cursor: pointer;
        }
      }
    }
  }
  .order-wrap {
    font-size: 14px;
    color: #666666;
    margin-top: 20px;
    height: 50px;
    line-height: 50px;
    .cart-tip {
      margin-left: 29px;
      a {
        color: #666666;
        margin-right: 37px;
      }
      span {
        color: #ff6600;
        margin: 0 5px;
      }
    }
    .total {
      font-size: 14px;
      color: #ff6600;
      span {
        font-size: 24px;
      }
      a {
        width: 202px;
        height: 50px;
        line-height: 50px;
        font-size: 18px;
        margin-left: 37px;
      }
    }
  }
}
</style>
